#include "stdafx.h"
#include "MaterialProject.h"

namespace Lit
{
	namespace Editor
	{
		namespace Material
		{
			void Project::on_actionAddExistingItem_triggered(){
				bool good = false;
				static const std::string filters = "All Ogre Scripts Files ( *.material; *.program; *.cg; *.glsl; *.hlsl; *.asm; *.compositor; );;";
				FilePath p;

				while( !good )
				{
					p = FileDialog::getOpenFileName( NULL, FROM_QT(tr("Add Existing File")), getFilePath(""), filters );
					if( !p.exists() ) return;
					if( hasFile( p.fileName() ) )
					{
						Dialog::warn( this, "File Duplicate", "File '"+p.fileName()+"' already exists in project !" );
						good = false;
					}
					else good = true;
				}
				try
				{
					createFile( p.fileName(), FileStream( p, Stream::TextRead ).readAll() );
				}
				catch( std::exception & e )
				{
					Dialog::warn( this, "Error", "Cannot open file '"+p.fileName()+"' because: "+std::string(e.what()) );
				}
				catch( ... )
				{
					Dialog::warn( this, "Error", "Cannot open file '"+p.fileName()+"' because unknown error happened !" );
				}
			}
			void Project::on_actionAddNewItem_triggered(){
				createFile();
			}
			void Project::on_actionAddFilter_triggered(){
			}
			void Project::on_actionRemoveItem_triggered(){
				QTreeWidgetItem * cur = mTree->currentItem(); if( !cur ) return;
				ProjectFilePtr f(getFileFromTree(cur)); if( !f ) return;
				QMessageBox msgBox;
				msgBox.setText(tr("What to do with the local copy ?"));
				msgBox.setInformativeText(TO_QT(f->getName()));
				QAbstractButton *noButton = msgBox.addButton(tr("Nothing"), QMessageBox::YesRole);
				QAbstractButton *yesButton = msgBox.addButton(tr("Remove"), QMessageBox::NoRole);
				QAbstractButton *cancelButton = msgBox.addButton(tr("Cancel"), QMessageBox::NoRole);
				msgBox.setIcon(QMessageBox::Question);
				msgBox.exec();
				
				if(msgBox.clickedButton() == cancelButton) return;

				try
				{
					removeFile( f->getName(), (msgBox.clickedButton() == yesButton)?true:false );
				}
				catch( std::exception & e )
				{
					Dialog::warn( this, "Error", "Cannot remove file '"+f->getName()+"' because: "+std::string(e.what()) );
				}
				catch( ... )
				{
					Dialog::warn( this, "Error", "Cannot remove file '"+f->getName()+"' because unknown error happened !" );
				}
			}
			void Project::on_actionItemProperties_triggered(){
			}

			void Project::treeItemDoubleClicked( QTreeWidgetItem * item, int col )
			{
				for( auto i=mFiles.begin(); i!=mFiles.end(); i++ )
				{
					if( i->second->getItem() == item )
					{
						openFile( i->first );
					}
				}
			}
			void Project::treeItemClicked( QTreeWidgetItem* itm, int col )
			{
				updateUi();
			}
		};
	};
};